#include "core.h"

CDataProcess::CDataProcess()
{
	Initialize("");
}

CDataProcess::~CDataProcess()
{

}

CDataProcess* CDataProcess::getInstance()
{
	static CDataProcess *p = NULL;
	if (!p)
	{
		p = new CDataProcess;
	}
	return p;
}

bool CDataProcess::Initialize(std::string host)
{
	m_strUrl = "http://";
	m_strUrl += host;
	m_strUrl += "/index/doc";
	return true;
}

bool CDataProcess::Destory()
{
	delete this;
	return true;
}

STDataProcessResponse* CDataProcess::dataProcess(STDataProcessRequest *pRequest)
{
	std::string requestUrl = m_strUrl;
	requestUrl += "/";
	requestUrl += pRequest->appname;
	requestUrl += "?";

	Json::FastWriter writer;
	Json::Value root;
	Json::Value item;
	Json::Value fields;

	std::vector<STDataItemInfo>::iterator it = pRequest->items.begin();
	for (; it!=pRequest->items.end(); it++)
	{
		struct timeval tv;    
		gettimeofday(&tv,NULL);    
		uint32_t timestamp = tv.tv_sec * 1000 + tv.tv_usec / 1000; 

		item["cmd"] = it->cmd;
		item["timestamp"] = timestamp;
		fields["id"] = it->id;
		fields["title"] = it->title;
		fields["body"] = it->body;
		item["fields"] = fields;
		root.append(item);
	}
	std::string json_value = writer.write(root);

	m_mapRequestParameter = mapRequestParameter;
	m_vecRequestParameter = vecRequestParameter;
	addQuerryParameter("action", pRequest->action, m_mapRequestParameter, m_vecRequestParameter);
	addQuerryParameter("table_name", pRequest->table_name, m_mapRequestParameter, m_vecRequestParameter);
	addQuerryParameter("items", json_value, m_mapRequestParameter, m_vecRequestParameter);

	std::string sign;
	GetQuerryParameter(m_mapRequestParameter, m_vecRequestParameter, "POST", sign);

	m_mapRequestParameter.erase("items");
	std::vector<std::string>::iterator itvec = m_vecRequestParameter.begin();
	for (; itvec!=m_vecRequestParameter.end();)
	{
		if ((*itvec) == "items")
		{
			itvec = m_vecRequestParameter.erase(itvec);
		}
		else
		{
			itvec++;
		}
	}

	uint32_t count = 0;
	for (itvec=m_vecRequestParameter.begin(); itvec!=m_vecRequestParameter.end(); count++, itvec++)
	{
		requestUrl += urlencode(*itvec);
		requestUrl += "=";
		requestUrl += urlencode(m_mapRequestParameter[itvec->c_str()]);

		if (count != m_vecRequestParameter.size()-1)
		{
			requestUrl += "&";
		}
	}

	requestUrl += "&";
	requestUrl += urlencode(std::string("Signature"));
	requestUrl += "=";
	requestUrl += sign;

	std::string tmp = "items=";
	tmp += urlencode(json_value);
	if (!Request(requestUrl, "POST", tmp))
	{
		printf("[%s]:%d\n", __FUNCTION__, __LINE__);
		return NULL;
	}

	parsedataProcessResponse();
	return &m_dataProcessResponse;
}

bool CDataProcess::parsedataProcessResponse()
{
	Json::Reader reader;
	Json::Value result;
	printf("%s\n", g_buf_tmp.data);
	if (reader.parse(g_buf_tmp.data, result))
	{
		m_dataProcessResponse.status = result["status"].asString();
		if (m_dataProcessResponse.status != "OK")
		{
			uint32_t count = result["errors"].size();
			Json::Value errors = result["errors"];
			for (uint32_t i=0; i<count; i++)
			{
				STErrorInfo errorInfo;
				errorInfo.errorCode = errors[i]["code"].asUInt();
				errorInfo.errorMessage = errors[i]["message"].asString();
				m_dataProcessResponse.errorList.push_back(errorInfo);
			}
		}
		m_dataProcessResponse.request_id = result["request_id"].asString();
	}
	return true;
}